package cn.fl.ref.asspackinfo.utils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.fl.ref.asspackinfo.vo.AssPackListVO;
import cn.lb.framework.poi.excel.ExcelImportUtil;
import cn.lb.framework.poi.excel.entity.ImportParams;

public class ImportAssPackList {

	private static final Logger logger = LoggerFactory.getLogger(ImportAssPackList.class);
	
	/**
	 * 导入资产包支付ID
	 * @param filePath
	 * @return
	 */
	public static Map<String,Object> importXls(String filePath){
		logger.info("导入资产包支付ID="+filePath);
        ImportParams params = new ImportParams();
        params.setTitleRows(0);//标题占用行数
        params.setHeadRows(1);//表头占用行数
        params.setNeedSave(false);//是否保存文件
        
        Map<String,Object> msgMap = new HashMap<String,Object>();
        List<AssPackListVO> list = null;
        
        if(!filePath.contains("http")){
            //File file=new File(PropertiesCacheUtils.getProperty(SysUlFilesRecConstant.SYS_FILE_UPLOAD_ROOT_PATH)+filePath);
            File file=new File(filePath);
            if(file.exists()){
                list = ExcelImportUtil.importExcel(file, AssPackListVO.class, params);
            }
        }else{
            InputStream in= null;
            URLConnection connection=null;
            try {           
            	URL url=new URL(filePath);
                connection=url.openConnection();//打开连接 获得URLConnection类的对象
                connection.setConnectTimeout(10000);
                connection.setReadTimeout(60000);
                in=connection.getInputStream();
                list = ExcelImportUtil.importExcel(in, AssPackListVO.class, params);
            } catch (IOException e) {
                BaseLogUtils.error(logger,"importXls","导入资产包支付表编号读取上传文件出错，"+e.getMessage());
			BaseLogUtils.error(logger, "-", "异常", e);
            } catch (Exception e) {
                BaseLogUtils.error(logger,"importXls","导入资产包支付表编号读取上传文件出错,"+e.getMessage());
			BaseLogUtils.error(logger, "-", "异常", e);
            } finally {
                if(null!=in){
                    try {
                        in.close();
                    } catch (IOException e) {
			BaseLogUtils.error(logger, "-", "异常", e);
                    }
                }
            }
        }
        
        //查询支付表所有有效的支付ID
        String payIds = "";
        StringBuffer errMsg = new StringBuffer("");
        if(CollectionUtils.isNotEmpty(list)){
            BaseLogUtils.info(logger,"importXls","导入资产包支付表编号路径="+filePath+",记录条数="+list.size());
            for(int i = 0; i < list.size(); i++){
            	if(("").equals(payIds)){
            		payIds = "'" + String.valueOf(list.get(i).getPaySchId()) + "'";
            	}else{
            		payIds += ",'" + String.valueOf(list.get(i).getPaySchId()) + "'";
            	}
            }
        }else{
            BaseLogUtils.info(logger,"importXls","导入资产包支付表编号路径="+filePath+",记录条数=0");
        }
        msgMap.put("errMsg", errMsg);
        msgMap.put("payIds", payIds);
        return msgMap;
	}
	
}
